<<template>
    <div class="app-container">

    <el-form :inline="true"  class="demo-form-inline">
      <el-form-item label="品牌名称">
      <el-input @clear="search" v-model="searchMap.where.name" placeholder="请输入品牌" clearable></el-input>
    </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="success" @click="clearFiles()" plain>添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="delBatch()" plain>批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="brandList" style="width: 100%" ref="brandTable">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="name"
        label="品牌名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="logo"
        label="品牌logo"
        width="180">
          <template slot-scope="scope">
            <div>
               <img  style="width:100px;height:40px" :src="scope.row.logo">
            </div>
          </template>
      </el-table-column>
      <el-table-column
        prop="firstLetter"
        label="首字母"
        width="180">
      </el-table-column>
      <el-table-column
        prop="showStatus"
        label="是否展示">
        <template slot-scope="scope">
            {{scope.row.showStatus == 1 ? '展示':'下架'}}
          </template>
      </el-table-column>
      <el-table-column
        prop="productCommentCount"
        label="产品评论数量">
      </el-table-column>
      <el-table-column
        prop="productCount"
        label="产品数量">
      </el-table-column>

       <el-table-column width="200px" 
            fixed = "right"
            label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="success" icon="el-icon-edit" @click="updateBrand(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="delBrand(scope.row.id)" >删除</el-button>
          </template>
        </el-table-column>
    </el-table>
<!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.page"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="searchMap.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 品牌添加 -->

    <el-dialog title="品牌添加" :visible.sync="dialogFormVisible">
      <el-form :model="brand" :rules="rules" ref="brandFrom"  >
        <el-form-item   label="品牌名称"  label-width=80px  prop="name">
          <el-input v-model="brand.name"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item  label="首字母" label-width=80px  prop="firstLetter">
          <el-input v-model="brand.firstLetter" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item  label="是否展示" label-width=80px prop="showStatus">
          <el-radio-group v-model="brand.showStatus" >
            <el-radio :label="1" >展示</el-radio>
            <el-radio :label="0">下架</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="品牌图片" label-width=80px prop="logo">
          <el-upload
            ref="aa"
            name="headImg"
            action="http://localhost:9999/upload"
            list-type="picture-card"
            :on-success="fileUpload">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save('brandFrom')">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 品牌修改 -->

     <el-dialog title="品牌修改" :visible.sync="dialogFormVisible2">
      <el-form :model="brand" :rules="rules" ref="brandFrom"  >
        <el-form-item   label="品牌名称"  label-width=80px  prop="name">
          <el-input v-model="brand.name"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item  label="首字母" label-width=80px  prop="firstLetter">
          <el-input v-model="brand.firstLetter" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item  label="是否展示" label-width=80px prop="showStatus">
          <el-radio-group v-model="brand.showStatus" >
            <el-radio :label="1" >展示</el-radio>
            <el-radio :label="0">下架</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="品牌图片" label-width=80px prop="logo">
          <el-upload
            name="headImg"
            action="http://localhost:9999/upload"
            list-type="picture-card"
            :on-success="fileUpload">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="upBrand('brandFrom')">确 定</el-button>
      </div>
    </el-dialog>
   
        
    </div>
</template>

<script>
import { selectPage, delById ,save,delBatch,findOne,updateBrand} from "../api/brand";
export default {
  data() {
    return {
      searchMap: {
        page: 1,
        size: 5,
        where: {},
      },
      brandList: [],
      pages: 0,
      total: 0,
      dialogFormVisible:false,//对话框的显示与隐藏ng
      dialogFormVisible2:false,
      brand:{
        name: '',
        firstLetter: '',
        showStatus: 1,
        logo: '',

      },
      rules: {
        name: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' }
        ],
        firstLetter: [
          { required: true, message: '请输入首字母', trigger: 'blur' }
        ],
        showStatus: [
          { required: true, message: '请选择展示状态', trigger: 'change' }
        ],
        logo: [
          { required: true, message: '请上传图片', trigger: 'change' }
        ],
      },
     
    };
  },
  methods: {
    //查询品牌列表
    search() {
      selectPage(this.searchMap).then((res) => {
        this.brandList = res.data;
        this.pages = res.pages;
        this.total = res.total;
      });
    },
    //删除单个
    delBrand(id) {
      console.log(id);
      
      this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            delById(id).then((res) => {
              if (res.success) {
                this.$notify({
                  title: '成功',
                  message: res.msg,
                  type: 'success'
                });
              }
              this.search();
            })
        })
    },
    //批量删除
    delBatch(){
       console.log(this.$refs.brandTable.selection);
       this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            delBatch(this.$refs.brandTable.selection).then(res=>{
                if(res.success){
                  this.$notify({
                      title: '成功',
                      message: res.msg,
                      type: 'success'
                    });
                }
                this.search();
            })
        })
    },
    //清空表单数据
    clearFiles(){
       
        this.brand ={
          showStatus: 1,
        };
        if(this.$refs.aa){
          this.$refs.aa.clearFiles();
        }
         this.dialogFormVisible = true;
       
    },
    //编辑品牌  1. 数据回显
    updateBrand(id){
      findOne(id).then(res => {
        if(res.success){
            this.brand = res.data;
        }
      })
      this.dialogFormVisible2 = true;

    },
    //编辑品牌  1. 修改品牌
    upBrand(formName){
      //表单校验
      this.$refs[formName].validate((valid) => {
          if (valid) {
            updateBrand(this.brand).then(res =>{
              if(res.success){
                this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success'
                });
                this.dialogFormVisible2 = false;
                this.search();
              }else{
                this.$notify({
                title: '修改失败',
                message: res.msg,
                type: 'error'
                });
              }
            })
          } 
        });
    },

    //保存品牌
    save(formName){
     
      //表单校验
      this.$refs[formName].validate((valid) => {
          if (valid) {
            save(this.brand).then(res =>{
              if(res.success){
                this.$notify({
                title: '成功',
                message: '保存成功',
                type: 'success'
                });
                this.dialogFormVisible = false;
                this.search();
              }else{
                this.$notify({
                title: '保存失败',
                message: res.msg,
                type: 'error'
                });
              }
            })
          } 
        });

      
    },
    //文件上传
    fileUpload(response, file, fileList){
      console.log(response);
      //获取图片地址
      this.brand.logo = response.data
    },
    //size变化的时候触发
    handleSizeChange(size) {
      this.searchMap.size = size;
      this.search();
    },
    //当前页发生变化的时候触发
    handleCurrentChange(page) {
      this.searchMap.page = page;
      this.search();
    },
  },
  created() {
    this.search();
  },
};
</script>
